package com.leoman.service.impl;

import com.leoman.core.Constant;
import com.leoman.dao.PersonalAuthDao;
import com.leoman.entity.PersonalAuth;
import com.leoman.service.PersonalAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * PersonalAuthServiceImpl
 * Created by 涂奕恒 on 2016/10/18 0018 下午 3:23.
 */
@Service
public class PersonalAuthServiceImpl implements PersonalAuthService {

    @Autowired
    private PersonalAuthDao personalAuthDao;

    @Override
    public List<PersonalAuth> findAll() {
        return personalAuthDao.findAll();
    }

    @Override
    public Page<PersonalAuth> find(int pageNum, int pageSize) {
        return personalAuthDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<PersonalAuth> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public PersonalAuth getById(int id) {
        return personalAuthDao.findOne(id);
    }

    @Override
    public void deleteById(int id) {
        personalAuthDao.delete(id);
    }

    @Override
    public PersonalAuth create(PersonalAuth personalAuth) {
        return personalAuthDao.save(personalAuth);
    }

    @Override
    public PersonalAuth update(PersonalAuth personalAuth) {
        return personalAuthDao.save(personalAuth);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }

    @Override
    public PersonalAuth findOneByCompanyUserId(Integer companyUserId) {
        return personalAuthDao.findOneByCompanyUserId(companyUserId);
    }
}